package logic.ships.mothership;

import java.util.ArrayList;
import java.util.List;

import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.TriMesh;

import logic.SpawnLocation;
import logic.nodes.TeamNode;
import logic.slots.Slot;
import logic.slots.SlotController;

public class SpawnSlot extends SpawnLocation implements Slot {
		
	private TriMesh slotMesh;
	private TriMesh[] meshArray;
	private List<TeamNode> nodes;
	private MotherShip ms;
	private SlotController slotController;
	
	public SpawnSlot(TriMesh slotMesh, float orientation, MotherShip ms) {
		super(orientation);
		this.slotMesh = slotMesh;
		this.ms = ms;
		
		meshArray = new TriMesh[] { slotMesh };
		
		ms.registerSlot(this);
		
		nodes = new ArrayList<TeamNode>();
		
		slotController = new SlotController(this);
		slotMesh.addController(slotController);
	}
	
	@Override
	public void checkSlots() {
		Node parent = slotMesh.getParent();
		Vector3f center = slotMesh.getWorldBound().getCenter();
		spawnLoc = new Vector3f(center);
		parent.localToWorld(spawnLoc, spawnLoc);
		generateSpawns(spawnLoc);
		
		parent.detachChild(slotMesh);
		parent.updateGeometricState(0f, false);
		
		for(TeamNode node : nodes) {
			node.getLocalTranslation().set(getRandomSpawn());
			node.getIngameState().addNode(node);
		}
	}
	
	public void addNode(TeamNode node) { nodes.add(node); }
	
	@Override
	public void removeSlotController() { slotMesh.removeController(slotController); }
	
	public TriMesh getSlotMesh() { return slotMesh; }
	
	@Override
	public void unregisterSlot() {
		ms.slotDone(this);
	}
	
	@Override
	public TriMesh[] getSlotMeshs() { return meshArray; }
}